package com.dz.controller.manager;

import com.dz.annotation.OperationLog;
import com.dz.bean.Manager;
import com.dz.service.ManagerService;
import com.dz.util.Page;
import com.dz.util.PasswordUtil;
import com.dz.util.Result;
import com.dz.util.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@CrossOrigin//允许跨域访问
@RequestMapping("/m/manager")
public class ManagerController {

    @Autowired
    ManagerService managerService;

    @GetMapping("page")
    public Result page(Page page, Manager manager) {
        managerService.page(page, manager);
        return Result.success(page);
    }

    @PostMapping
    @OperationLog("添加管理员")
    public Result add(@RequestBody Manager manager) {
        // 设置默认密码并加密
        String defaultPassword = "123456"; // 默认密码
        manager.setPassword(PasswordUtil.getEncryption(defaultPassword));

        Object obj = managerService.add(manager);
        return Result.success(obj);
    }

    @DeleteMapping("/{id}")
    @OperationLog("删除管理员")
    public Result delete(@PathVariable("id") Integer id){
        Object obj = managerService.delete(id);
        return Result.success(obj);
    }

    @PutMapping
    @OperationLog("修改管理员信息")
    public Result edit(@RequestBody Manager manager) {
        Object obj = managerService.edit(manager);
        return Result.success(obj);
    }

    @GetMapping("all")
    public Result all(Manager manager) {
        Object obj = managerService.all(manager);
        return Result.success(obj);
    }

    @GetMapping("{id}")
    public Result findById(@PathVariable("id") Integer id) {
        Object obj = managerService.findById(id);
        return Result.success(obj);
    }

    @PutMapping("/changePassword")
    @OperationLog("管理员修改密码")
    public Result changePassword(@RequestBody Map<String, String> passwordData, HttpServletRequest request) {
        try {
            // 从token中获取当前用户信息
            String token = request.getHeader("token");
            Integer managerId = TokenUtil.getId(token);

            // 获取原密码和新密码
            String oldPassword = passwordData.get("oldPassword");
            String newPassword = passwordData.get("newPassword");

            if (oldPassword == null || newPassword == null) {
                return Result.error(400,"密码不能为空");
            }

            // 获取当前管理员信息
            Manager currentManager = managerService.findById(managerId);
            if (currentManager == null) {
                return Result.error(400,"管理员不存在");
            }

            // 验证原密码
            if (!PasswordUtil.checkPassword(oldPassword, currentManager.getPassword())) {
                return Result.error(400,"原密码错误");
            }

            // 更新密码
            currentManager.setPassword(PasswordUtil.getEncryption(newPassword));
            managerService.edit(currentManager);

            return Result.success(200,"密码修改成功");
        } catch (Exception e) {
            return Result.error(400,"密码修改失败: " + e.getMessage());
        }
    }

}
